// room相关业务逻辑
import ErrorType from '../../constants/errorType'
import { Shards } from '../../constants/screeps'
import { IGroupMessageEvent } from '../../interface/bot.interface'
import { Shard } from '../../interface/screeps.interface'
import { getActivePvpRoom, getNuke, getRoomObject } from '../../request/screeps'
import { format, isShard, splitLine, wrapCommandFn } from '../../utils'
import { Next } from '../app'

/**
 * 查看正在发生pvp房间
 */
export const lookPvp = wrapCommandFn(
    async (e: IGroupMessageEvent, next: Next) => {
        const res = await getActivePvpRoom(Number(e.args[0]) || 10)
        let shard = e.args[1] && (e.args[1].toLowerCase() as Shard)
        if (shard && !isShard(shard)) return
        else if (!shard) shard = 'shard3'
        if (res.data.ok) {
            const rooms = res.data.pvp[shard].rooms.map((room) => room._id)
            e.group.sendMsg(`[${shard}]正在pvp的房间有：` + rooms.join(' '))
        }
        next()
    },
    () => {
        return format('/pvp [interval] [shard]', ['interval: 间隔时间', 'shard: 分片'], '查看正在pvp的房间')
    },
)

/**
 * 查看核弹信息
 */
export const lookNuke = wrapCommandFn(async (e: IGroupMessageEvent, next: Next) => {
    const res = await getNuke()
    if (res.data.ok) {
        const strs = ['正在发射的核弹：']
        for (const shard of Shards) {
            strs.push(`[${shard}]：`)
            res.data.nukes[shard].forEach((item) => {
                strs.push(`${item.launchRoomName} -> ${item.room}(${item.x},${item.y})`)
            })
        }
        e.group.sendMsg(splitLine(strs))
    }
    next()
})

/**
 * 获取房间对象
 */
export const lookRoomObject = wrapCommandFn(
    async (e: IGroupMessageEvent, next: Next) => {
        try {
            if (!isShard(e.args[1])) return
            const res = await getRoomObject(e.args[0], e.args[1] as Shard)
            if (res.data.ok) {
                const type = e.args[2]
                for (const object of res.data.objects) {
                    if (object.type === type) {
                        e.group.sendMsg(JSON.stringify(object))
                        break
                    }
                }
            }
            next()
        } catch (error) {
            e.group.sendMsg(ErrorType.REQUEST_ERROR)
        }
    },
    () => {
        return format(
            '/object [room] [shard] [type]',
            ['room: 房间名', 'shard: 分片', 'type: 对象类型'],
            '获取房间中对象信息',
        )
    },
)

/**
 * 获取房间所属的用户
 */
export const lookRoomOwner = wrapCommandFn(
    async (e: IGroupMessageEvent, next: Next) => {
        try {
            if (!isShard(e.args[1])) return
            const res = await getRoomObject(e.args[0], e.args[1] as Shard)
            if (res.data.ok) {
                const user = Object.values(res.data.users).find((item) => item.badge)
                if (user) {
                    e.reply(`房间${e.args[0]}所属用户为：${user.username}`)
                } else {
                    e.reply('未找到该房玩家')
                }
            }
            next()
        } catch (error) {
            e.group.sendMsg(ErrorType.REQUEST_ERROR)
        }
    },
    () => {
        return format('/owner [room] [shard]', ['room: 房间名', 'shard: 分片'], '获取房间所属用户')
    },
)
